package com.bridge.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.regex.Matcher;
import java.util.regex.Pattern;


import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * @ClassName EditTextUtils
 * @Description TODO消息
 * @Author xushufeng
 * @CreateDate 2021/3/24 0024 10:06
 * @Version 1.0
 * @UpdateDate 2021/3/24 0024 10:06
 * @UpdateRemark 更新说明
 */
public class EditTextUtils {
    private static EditTextUtils editTextUtils;

    public static EditTextUtils getEditTextUtils() {
        if (editTextUtils == null) {
            editTextUtils = new EditTextUtils();
        }
        return editTextUtils;
    }



    /**
     * 显示键盘
     *
     * @param et 输入焦点
     */
    public void showInput(final EditText et,Context activity) {
        et.requestFocus();
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }


    public void setFocusable(EditText editText, Activity activity) { //获取焦点，弹出软键盘
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    public void hideInputMode(Activity activity){
//        InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
    public boolean equalsNumber(String string) {
        Boolean b = string.matches(".*?[0-9]+.*?");
        return b;
    }

    public boolean equalsZiMu(String string) {
        Boolean b = string.matches(".*?[a-zA-Z]+.*?");
        return b;
    }


    /**
     * 匹配 111 型 (\d)\1{2}
     *
     * 匹配  1122 型  (\d)\1{1}(\d)\2{1}
     *
     *
     * 匹配 123 型  ((?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){2}\d)
     * @param string
     * @return
     */
//    public static boolean equalsContinuousNumber(String string) {
//        Boolean b = string.matches("(\\d)\\1{2}");
//        return b;
//    }
//
//    public static boolean equalsDuplicateNumber(String string) {
//        Boolean b = string.matches("((?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){2}\\d)");
//        return b;
//    }


    /**
     * ^(.)\1*$|^\d(?:(?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9)*$
     *
     *
     *
     * ^(.)\1*$  是 匹配111111全部重复的
     *
     * ^\d(?:(?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9)*$  是匹配递增的这种连续数字，如果还要加上递减的（如654321），那可以模仿这个写
     * @param string
     * @return
     */

    public static boolean equalsContinuousNumber(String string) {
        Boolean b = string.matches("^(.)\\1*$|^\\d(?:(?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9)*$");
        return b;
    }

    public static boolean equalsDuplicateNumber(String string) {
        Boolean b = string.matches("^\\d(?:(?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9)*$");
        return b;
    }
//    public static boolean equalsDuplicateNumber2(String string) {
//        Boolean b = string.matches("^\\d(?:(?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9)*$");
//        return b;
//    }

    public boolean equalsHanZi(String string) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(string);
        if (m.matches()) {
            return true;
        }
        return false;
    }

    public boolean equalNumberZiMu(String testStr) {//有字母和数字
        Boolean b = testStr.matches(".*?[a-zA-Z]+.*?") && testStr.matches(".*?[\\d]+.*?");

        return b;
    }

    public static boolean isStringEmpty(String string) {
        return string == null || "".equals(string);
    }


    public String setPhone(String phone) {
        if (!TextUtils.isEmpty(phone))
        phone = phone.replaceAll("\\s*", "");
        return phone;
    }


    public void setImageBackTextColor(ImageView imageView, TextView textView, Drawable drawable, int color){
        imageView.setImageDrawable(drawable);
        textView.setTextColor(color);
    }

}
